/*
 * The MIT License (MIT)
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

use napi::bindgen_prelude::*;
use napi_derive::napi;

#[napi]
fn map_option(val: Option<u32>) -> Option<u32> {
  val.map(|v| v + 1)
}

#[napi]
fn return_null() -> Null {
  Null
}

#[napi]
fn return_undefined() -> Undefined {}

#[napi(object, use_nullable = true)]
pub struct UseNullableStruct {
  pub required_number_field: u32,
  pub required_string_field: String,
  pub nullable_number_field: Option<u32>,
  pub nullable_string_field: Option<String>,
}

#[napi]
pub fn rust_use_nullable_struct(s: UseNullableStruct) -> String {
  s.required_string_field.clone()
    + s
      .nullable_string_field
      .unwrap_or("[rust]".to_string())
      .as_str()
}

#[napi(object, use_nullable = false)]
pub struct NotUseNullableStruct {
  pub required_number_field: u32,
  pub required_string_field: String,
  pub optional_number_field: Option<u32>,
  pub optional_string_field: Option<String>,
}

#[napi]
pub fn rust_not_use_nullable_struct(s: NotUseNullableStruct) -> String {
  s.required_string_field.clone()
    + s
      .optional_string_field
      .unwrap_or("[rust]".to_string())
      .as_str()
}

#[napi(object)]
struct DefaultUseNullableStruct {
  pub required_number_field: u32,
  pub required_string_field: String,
  pub optional_number_field: Option<u32>,
  pub optional_string_field: Option<String>,
}

#[napi(constructor, use_nullable = true)]
pub struct UseNullableClass {
  pub required_number_field: u32,
  pub required_string_field: String,
  pub nullable_number_field: Option<u32>,
  pub nullable_string_field: Option<String>,
}

#[napi]
pub fn rust_use_nullable_class(s: &mut UseNullableClass) -> String {
  s.required_number_field += 1;
  s.required_string_field += "[add in rust]";
  if s.nullable_string_field.is_none() {
    s.nullable_string_field = Some("[rust]".to_string());
  }
  s.required_string_field.clone() + s.nullable_string_field.clone().unwrap().as_str()
}

#[napi(constructor, use_nullable = false)]
pub struct NotUseNullableClass {
  pub required_number_field: u32,
  pub required_string_field: String,
  pub optional_number_field: Option<u32>,
  pub optional_string_field: Option<String>,
}

#[napi]
pub fn rust_not_use_nullable_class(s: &mut NotUseNullableClass) -> String {
  s.required_number_field += 1;
  s.required_string_field += "[add in rust]";
  if s.optional_string_field.is_none() {
    s.optional_string_field = Some("[rust]".to_string());
  }
  s.required_string_field.clone() + s.optional_string_field.clone().unwrap().as_str()
}

#[napi(constructor)]
struct DefaultUseNullableClass {
  pub required_number_field: u32,
  pub required_string_field: String,
  pub optional_number_field: Option<u32>,
  pub optional_string_field: Option<String>,
}
